using Assets.Framework.WooEngineExtends.Config;
using System;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using WooEngine.Common;
using WooEngine.Extends.Root;
using WooEngine.Persistence;
using WooEngine.Persistence.Provider;
using WooEngine.Register;
using WooEngine.Res.Provider;
using WooEngine.UI;

namespace WooEngine.Extends.Main
{
    public class GameManager : SingletonBehaviour<GameManager>
    {
        public const string GameDBName = "GameDB";

        private AppConfigProxy m_appConfigProxy;
        private LogicRoot m_LogicRoot;
        private UIRoot m_UIRoot;

        public LogicRoot GameLogicRoot { get => m_LogicRoot; }
        public UIRoot GameUIRoot { get => m_UIRoot; }

        public void Init()
        {
            initAppConfig();

            initManager();

            initRegist();

            initUIRoot(()=>
            {
                PageManager.Instance.Init(GameUIRoot.UICanvasRoot);

                initLogicRoot(()=>
                {
                    Debug.Log("over");
                    SendNotification(FirstPageShowCommand.DefaultClassName);
                });
            });
        }

        private void initAppConfig()
        {
            DBManager.Instance.CreateProvider<EasySaveProvider>(GameDBName, GameDBName);

            m_appConfigProxy = new AppConfigProxy();


#if UNITY_IOS || UNITY_ANDROID
            Application.targetFrameRate = m_appConfigProxy.GetData().AppFrameRate;
#else
                        Screen.SetResolution (m_appConfigProxy.GetData().OutPX_W, m_appConfigProxy.GetData().OutPX_H, false, m_appConfigProxy.GetData().AppFrameRate);
#endif
        }

        private void initRegist()
        {
            RegisterManager.Instance.RegistProvider<FirstRegisterProvider>(FirstRegisterProvider.DefaultClassName);
        }

        private void initManager()
        {
            AddressableProvider provider = new AddressableProvider();
            Res.ResourceManager.Instance.Initialize(provider);
            Res.UpdateManager.Instance.Initialize(provider, "", "");
        }

        private void initUIRoot(Action callBack)
        {

            GameObject root_go = GameObject.Find("UIRoot");
            if (!root_go)
            {
                Res.ResourceManager.Instance.LoadAssetAsync<GameObject>("UIRoot", (GameObject go, object ob) =>
                {
                    root_go = go.Spawn(null);
                    m_UIRoot = root_go.GetComponent<UIRoot>();
                    callBack();
                });
            }
            else
            {
                m_UIRoot = root_go.GetComponent<UIRoot>();
                callBack();
            }
        }

        private void initLogicRoot(Action callBack)
        {
            GameObject root_go = GameObject.Find("LogicRoot");
            if (!root_go)
            {
                Res.ResourceManager.Instance.LoadAssetAsync<GameObject>("LogicRoot", (GameObject go, object ob) =>
                {
                    root_go = go.Spawn(null);
                    m_LogicRoot = root_go.GetComponent<LogicRoot>();
                    m_LogicRoot.LogicCamera.GetUniversalAdditionalCameraData().cameraStack.Add(m_UIRoot.UICamera);
                    callBack();
                });
            }
            else
            {
                m_LogicRoot = root_go.GetComponent<LogicRoot>();
                callBack();
            }
        }

    }
}